from rest_framework.decorators import action
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response

from apps.meiduo_admin.serializers.perms_serializer import PermissionSerializer
from apps.meiduo_admin.utils import NumPage
# djang的权限类,权限类型表
from django.contrib.auth.models import Permission, ContentType


class PermsView(ModelViewSet):
    # 指定查询集
    queryset = Permission.objects.all()
    # 指定序列化器
    serializer_class = PermissionSerializer
    # 指定权限
    permission_classes = [IsAdminUser]
    # 指定分页
    pagination_class = NumPage

    # 获取权限类型
    def content_types(self, request):
        # 查询所有权限类型
        contents = ContentType.objects.all()
        # 结果返回
        content_list = []
        for content in contents:
            content_list.append({
                'id': content.id,
                'name': content.name
            })
        return Response(content_list)